import 'dart:async';
import 'package:common_utils/common_utils.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter/material.dart';
import 'package:flutter_fortune_wheel/flutter_fortune_wheel.dart';
import 'package:yp/widgets/dialog.dart';
import 'package:yp/widgets/index.dart';

/// 大转盘  没有具体界面 单独拿出来做了一个
class RoundCom extends StatefulWidget {
  const RoundCom({Key key, this.endAngle,this.rotated = false, this.child, this.duration}) : super(key: key);
    final double endAngle; // 旋转角度
  final bool rotated;    //是否旋转
  final Widget child;
  final int duration;    //旋转时间

  @override
  _RoundComState createState() => _RoundComState();
}

class _RoundComState extends State<RoundCom> with SingleTickerProviderStateMixin {
    AnimationController _controller;
  Animation _animation;
  double angle = 0;
  StreamController<int> _streamController = StreamController<int>();

  final items = <String>[
    '100',
    '200',
    '500',
    '666',
  ];

  int _giftNum = 0;

 @override
  void initState() {
    // TODO: implement initState
    _controller = AnimationController(vsync: this,duration: Duration(milliseconds: widget.duration));
    _animation = Tween(begin: 0.0,end: widget.endAngle).animate(_controller)..addListener((){
      setState(() {
        print(  double.parse(NumUtil.getNumByValueDouble(_animation.value, 0).toStringAsFixed(0)));
        angle = double.parse(NumUtil.getNumByValueDouble(_animation.value, 0).toStringAsFixed(0));
      });
    });
    super.initState();
  }

  @override
  void didUpdateWidget(RoundCom oldWidget) {

    // TODO: implement didUpdateWidget

    if(oldWidget.rotated == widget.rotated){
      return; //减少绘制
    }
    if(!widget.rotated){
      _controller.reverse();//反向旋转
    }else{
      _controller.forward();//正向旋转
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _controller.dispose();
    super.dispose();
  }


  @override
  Widget build(BuildContext context) {
    return   Transform.rotate(angle: angle,child: widget.child);
  }
}
